﻿// HSS.ServiceModel.Internal.VirtualFaultBehavior.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       VirtualFaultBehavior.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.ServiceModel.Internal
{
	#region Using Directives
	using System;
	using System.ServiceModel;
	using System.ServiceModel.Channels;
	using System.ServiceModel.Configuration;
	using System.ServiceModel.Description;
	using System.ServiceModel.Dispatcher;
	#endregion

	#region VirtualFaultBehavior
#if !DEBUG
	[System.Diagnostics.DebuggerStepThroughAttribute()]
#endif
	internal class VirtualFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
	{
		#region IEndpointBehavior Members

		public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
		{

		}

		public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
		{

		}

		public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
		{
			endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new VirtualFaultMessageInspector());
		}

		public void Validate(ServiceEndpoint endpoint)
		{
		}

		#endregion

		#region Overrides
		/// <summary>
		/// Gets the Type of the Behavior
		/// </summary>
		public override Type BehaviorType
		{
			get { return typeof(VirtualFaultBehavior); }
		}
		/// <summary>
		/// Creates the Behavior
		/// </summary>
		/// <returns>The <see cref="VirtualFaultBehavior"/></returns>
		protected override object CreateBehavior()
		{
			return new VirtualFaultBehavior();
		}
		#endregion

		#region VirtualFaultMessageInspector
		private class VirtualFaultMessageInspector : IDispatchMessageInspector
		{
			#region IDispatchMessageInspector Members

			public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
			{
				// do nothing
				return null;
			}

			public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
			{
				if (reply.IsFault)
				{
					reply.Properties[HttpResponseMessageProperty.Name] = new HttpResponseMessageProperty
					{
						// Force a 200 response so the call to the Virtual Service actually returns to the 
						// caller where the error can be processed.
						StatusCode = System.Net.HttpStatusCode.OK // 200
					};
				}
			}

			#endregion
		}
		#endregion
	}
	#endregion
}